package com.zzf;

import org.apache.commons.collections.CollectionUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author wm
 * @description
 * @date 2021/6/22
 */

public class testDemo01 {

    private static SimpleDateFormat YMD = new SimpleDateFormat("yyyy-MM-dd");
    private static SimpleDateFormat MD = new SimpleDateFormat("yyyy-M-d");

    private static SimpleDateFormat YMDH = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private static SimpleDateFormat YM = new SimpleDateFormat("yyyy-M");
    private static SimpleDateFormat YMM = new SimpleDateFormat("yyyy-MM");
    private static final Logger logger = LoggerFactory.getLogger(testDemo.class);

    @Test
    public void test() {
        String sql = "select m.policyno, \n" +
                "t.inputtime gendate, \n" +
                "(select upperpath[19,26] from xm3502dms3gdb@xm_3502_cb_rss:prpdcompany where comcode = m.comcode) comcode, \n" +
                "(select comshortname from xm3502dms3gdb@xm_3502_cb_rss:prpdcompany where comcode = (select upperpath[19,26] from xm3502dms3gdb@xm_3502_cb_rss:prpdcompany where comcode = m.comcode)) comname, \n" +
                "c.licenseno, \n" +
                "c.vinno, \n" +
                "case when c.licenseno like '%闽D%' then  '1'  \n" +
                "when c.licenseno like '%闽E%' then '2'   \n" +
                "when c.licenseno like '%闽C%' then '3'  \n" +
                "when c.licenseno like '%闽F%' then '8'  \n" +
                "when c.licenseno like '%闽J%' then '9'  \n" +
                "when c.licenseno like '%闽A%' then '10' \n" +
                "when c.licenseno like '%闽B%' or c.licenseno like '%闽G%' or c.licenseno like '%闽H%' or c.licenseno like '%闽K%'   then '4' \n" +
                "else '5' end as licenseplate, \n" +
                "c.carkindcode, \n" +
                "case when c.clausetype in('F42','F47') then '0' \n" +
                "when c.clausetype = 'F41' and c.carkindcode='A01' then '1' \n" +
                "when c.clausetype = 'F41' and c.carkindcode!='A01' then '2' \n" +
                "when c.clausetype='F43' and c.carkindcode='A01' then '5' \n" +
                "when c.clausetype='F43' and c.carkindcode!='A01' then '3' \n" +
                "when c.clausetype='F44' then '6' \n" +
                "when c.clausetype='F46' then '4' \n" +
                "else '7' end as customer, \n" +
                "m.operatedate, \n" +
                "m.startdate, \n" +
                "m.enddate, \n" +
                "m.inserttimeforhis, \n" +
                "case when c.useyears=0 then '0' else '1' end as newoldcar,  	\n" +
                "m.sumpremium, \n" +
                "m.handlercode, \n" +
                "(select max(username) from xm3502ims3gdb@xm_3502_cb_rss:utiihandler where usercode=m.handlercode) handlername, \n" +
                "case when  m.remark like '%3502XB190006%' then '1' \n" +
                "when m.remark like '%3502SX210018%' then '2' \n" +
                "else '0' end as ishczx \n" +
                "from prpcmain m,prpcitem_car c,prpccarmodel cm,prpcinsured a,prpcinsured i,prpcmain_car b,prptime t \n" +
                "where m.proposalno=c.proposalno \n" +
                "and m.proposalno=cm.proposalno \n" +
                "and m.proposalno=a.proposalno \n" +
                "and m.proposalno=b.proposalno \n" +
                "and m.proposalno=i.proposalno \n" +
                "and m.policyno = t.certino  \n" +
                "and t.updatetype ='C01' \n" +
                "and a.insuredflag[1]='1' \n" +
                "and i.insuredflag[2]='1' \n" +
                "and m.underwriteflag in('1','3') \n" +
                "and c.clausetype!='F44' \n" +
                "and c.clausetype!='F45' \n" +
                "and m.riskcode!='DBC'  ";
        System.out.println(sql);
    }

    @Test
    public void test02() {
        String a = "禾山支公司";
        a = a.substring(0, a.length() - 3);
        System.out.println(a);
    }

    @Test
    public void test03() {
        String a = "";
    }

    @Test
    public void test04() {
        Date date = new Date();
        String format = YMM.format(date);
        System.out.println(format);
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -3);
        int actualMaximum = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println(actualMaximum);
        String format1 = YMDH.format(c.getTime());
        System.out.println(format1);
    }

    @Test
    public void test05() {
        String[] a = null;
        if (a != null && a.length > 0) {
            System.out.println(a.length);
        } else {
            System.out.println(2);
        }
    }

    @Test
    public void test06() {
        BigDecimal bigDecimal = new BigDecimal(-17.14);
        if (bigDecimal.compareTo(new BigDecimal(0)) == -1) {
            System.out.println("1");
        }
        if (bigDecimal.compareTo(new BigDecimal(0)) == 1) {
            System.out.println("2");
        }
    }

    @Test
    public void test07() {
        List<Object> arrayList = new ArrayList<>();
        arrayList.add("1");
        arrayList.add("2");
        arrayList.add("3");
        arrayList.add("4");
        List<List<Object>> list = new ArrayList<>();
        list = Collections.singletonList(arrayList);
        for (List<Object> objectList : list) {
            for (Object o : objectList) {
                System.out.println(String.valueOf(o));
            }
        }
    }

    @Test
    public void test08() {
        SimpleDateFormat sdf2 = new SimpleDateFormat("yyyyMM");
        Calendar c = Calendar.getInstance();
        c.setTime(new Date());
        c.add(Calendar.YEAR, -1);
        Date y = c.getTime();
        String year = sdf2.format(y);
        System.out.println(year);
    }

    @Test
    public void test09() {
        Random random = new Random();
        List<String> list = new ArrayList<>();
        for (int j = 0; j < 100; j++) {
            int i = random.nextInt(11);
            list.add(String.valueOf(i));
            //System.out.println(i);
        }
        Collections.sort(list, new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                int oo1 = Integer.parseInt(o1);
                int oo2 = Integer.parseInt(o2);
                return oo1 - oo2;
            }
        });
        System.out.println(list);
    }


    public void test10(int N) {
        /*int N = 10;
        int i;
        for (i = 1; i <= N; i++) {
            System.out.println(i);
        }*/
        if (N > 0) {
            test10(N - 1);
            System.out.println(N);
        }
        return;
    }

    @Test
    public void tset11() {
        this.test10(10);
    }


    public void test12(double n, double a[], double x) {
        double p = a[0];
        for (int i = 1; i <= n; i++) {
            p += a[i] * Math.pow(x, i);
            //System.out.println(p);
        }
        System.out.println(p);
    }

    @Test
    public void test13() {
        for (int i = 0; i < 10000; i++) {

        }
        double[] a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        test12(10, a, 2);
    }

    @Test
    public void test14() {
        String a = "1234,all,234";
        if (a.indexOf("all") == -1) {
            System.out.println("1");
        } else {
            System.out.println("2");
        }
        if (!a.contains("all")) {
            System.out.println("1");
        } else {
            System.out.println("2");
        }
    }

    @Test
    public void test15() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> hashMap = new HashMap<>();

        Map<String, Object> map = new HashMap<>();
        List<String> arrayList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            for (Map<String, Object> fNcarScheduleMap : list) {
                BigDecimal sumpremium2 = (BigDecimal) fNcarScheduleMap.get("sumpremium2");
                if (sumpremium2 != null) {
                    sumpremium2 = sumpremium2.setScale(0, BigDecimal.ROUND_DOWN);
                } else {
                    sumpremium2 = new BigDecimal(0);
                }
                arrayList.add(String.valueOf(sumpremium2));
            }
        }
        if (CollectionUtils.isNotEmpty(arrayList)) {
            int size = arrayList.size();
            //取数组的三分之二
            int size_s = (int) Math.ceil(size * 0.66);
            String sumpremium = arrayList.get(size_s - 1);
            //转为字符数组
            char[] charArray = sumpremium.toCharArray();
            //去掉最后一个字符，等会要拼接最后一个字符
            String sumpremium_string = "";
            for (int i = 0; i < charArray.length - 1; i++) {
                sumpremium_string = sumpremium_string + String.valueOf(charArray[i]);
            }
            //获取最后一个字符
            char sumpremium_char = charArray[sumpremium.length() - 1];
            //将字符先转为字符串,再转为int
            int sumpremium_int = Integer.parseInt(String.valueOf(sumpremium_char));
            //完成进度最低值
            BigDecimal sumpremium_low = new BigDecimal(0);
            //完成进度最高值
            BigDecimal sumpremium_mid = new BigDecimal(sumpremium_string + String.valueOf(sumpremium_char));
            BigDecimal sumpremium_high = new BigDecimal(0);
            //完成进度最后值
            int wcjd = 0;
            if (sumpremium_int > 0 && sumpremium_int < 5) {
                //如果该字符在0-5之间
                sumpremium_low = new BigDecimal(sumpremium_string + "0");
                sumpremium_high = new BigDecimal(sumpremium_string + "5");
            } else if (sumpremium_int > 5 && sumpremium_int < 10) {
                //该字符在5-10之间
                sumpremium_low = new BigDecimal(sumpremium_string + "5");
                sumpremium_high = new BigDecimal(sumpremium_string + "10");
            } else {
                //该字符等于0或者5或者10
                sumpremium_low = new BigDecimal(sumpremium_string + String.valueOf(sumpremium_char));
                sumpremium_high = new BigDecimal(sumpremium_string + String.valueOf(sumpremium_char));
            }
            //最低值和最高值一样，说明最后一个字符是0或者5或者10
            if (sumpremium_low.compareTo(sumpremium_high) == 0) {
                map.put("sumpremium_wcjd", sumpremium_mid);
            } else {
                int index_s = 0;
                int index_x = 0;
                //数组的上部分
                for (int i = 0; i < size_s; i++) {
                    BigDecimal surpremium_s = new BigDecimal(arrayList.get(i));
                    if (sumpremium_low.compareTo(surpremium_s) == -1 && sumpremium_mid.compareTo(surpremium_s) == 1) {
                        index_s++;
                    }
                }
                //数组的下部分
                int size_x = size - size_s;
                for (int i = 0; i < size_x; i++) {
                    BigDecimal surpremium_x = new BigDecimal(arrayList.get(i + size_s));
                    if (sumpremium_low.compareTo(surpremium_x) == -1 && sumpremium_mid.compareTo(surpremium_x) == 1) {
                        index_x++;
                    }
                }
                if (index_s == index_x) {
                    //个数相等,取最小
                    map.put("sumpremium_wcjd", sumpremium_low);
                } else if (index_s < index_x) {
                    //上行小于下行，取最小
                    map.put("sumpremium_wcjd", sumpremium_low);
                } else {
                    //上行大于下行，取最大
                    map.put("sumpremium_wcjd", sumpremium_high);
                }
            }
        } else {
            map.put("sumpremium_wcjd", new BigDecimal(0));
        }
    }

    @Test
    public void test16() {
        String str = "15865816620";
        String pattern = "(13\\d|14[579]|15[^4\\D]|17[^49\\D]|18\\d)\\d{8}";

        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        System.out.println(m.matches());
    }

    @Test
    public void test17() {
        String idCard = "421302199208165464";
        System.out.println(idCard.replaceAll("(\\d{4})\\d{8}(\\d{6})", "$1****$2"));

        String phone = "18790840883";
        System.out.println(phone.replaceFirst("(\\d{3})\\d{3}(\\d{5})", "$1****$2"));

        String str = "厦门禾山支公司";
        System.out.println(str.replaceAll("(\\d{2})", ""));
    }

    @Test
    public void test18() {
        String str = "厦门禾山支公司";
        System.out.println(str.substring(2, str.length()));
    }

    @Test
    public void test19() {
        for (int i = 0; i < 100; i++) {
            System.out.print(i);
        }
    }

    @Test
    public void test20() {
        String a = "abcd";
        char[] charArray = a.toCharArray();
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = charArray.length - 1; i >= 0; i--) {
            stringBuilder.append(charArray[i]);
        }
        //stringBuilder.reverse();
        System.out.println(stringBuilder);
    }

    @Test
    public void test21() {
        String a = "ABCDEFGHIAAJKLMNOPQRSTUVWXYZ";
        char[] chars = a.toCharArray();
        ArrayList<Map<String, Object>> list = new ArrayList<>();
        for (int i = 0; i < chars.length; i++) {
            int count = 0;
            HashMap<String, Object> hashMap = new HashMap<>();
            for (int j = 0; j < chars.length; j++) {
                if (String.valueOf(chars[i]).equals(String.valueOf(chars[j]))) {
                    count++;
                }
            }
            hashMap.put(String.valueOf(chars[i]), count);
            list.add(hashMap);
        }
        for (Map<String, Object> map : list) {
            System.out.println(map);
        }
    }

    @Test
    public void test22() {
        String a = "ABCDEFGHIAAJKLMNOPQRSTUVWXYZ";
        int[] count = new int[26];
        for (int i = 0; i < a.length(); i++) {
            char tmp = a.charAt(i);
            int index = tmp - 65;
            count[index] = count[index] + 1;
        }
        for (int i = 0; i < count.length; i++) {
            if (count[i] != 0) {
                System.out.println("字母" + (char) (i + 65) + "出现的次数：" + count[i]);
            }
        }
    }

    @Test
    public void test23() {
        int sum = 0;
        for (int i = 0; i < 100; i++) {
            sum += i;
        }
        System.out.println(sum);
    }
}
